package com.cqust.icc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/4/1 9:54
 */
@Component
public class JdbcTest {


    private static JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
        query();
        Update();
        getById();
    }

    private static void query() {
        List<LogEntity> query = jdbcTemplate.query("select * from tb_log",  new BeanPropertyRowMapper<LogEntity>(LogEntity.class));
        System.out.println(query);
    }

    public static void Update() {
        String sql = "UPDATE tb_log set method_name = 'update by zcc' WHERE zcc.tb_log.id =7";
        int update = jdbcTemplate.update(sql);
        if (update > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
    }

    public static void getById() {
        String sql = "select * from tb_log   WHERE tb_log.id =7";
        List<LogEntity> logEntityList = jdbcTemplate.query(sql, new Object[]{}, new BeanPropertyRowMapper<LogEntity>(LogEntity.class));
        if (logEntityList.size() > 0) {
            System.out.println(logEntityList.get(0));
        }


    }
}
